﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using YLClient.YLServiceClient;
using YLPublicService.TransferModel;
using YLClient.BLL;
using YLClient.Util;

namespace YLClient.Froms
{
    /// <summary>
    /// Interaction logic for Searcher.xaml
    /// </summary>
    public partial class Searcher : Window
    {
        public event Action<IEnumerable<IWorkItem>> OnResultArrive;

        public Searcher()
        {
            InitializeComponent();
            InitiUI();
        }

        private void InitiUI()
        {
            var templates = TemplateManager.GetTemplateManager().templates;
            foreach (var template in templates)
            {
                cmbTemplateName.Items.Add(template.mTemplateDetial.mName);
            }
            cmbTemplateName.SelectedIndex = 0;
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            WorkItemManager manager = new WorkItemManager();
            var template = TemplateManager.GetTemplateManager()
                                           .templates[cmbTemplateName.SelectedIndex];
            try
            {

                DateTime preDate = DateTime.Now;
                string str = (cmbPreDate.SelectedItem as ComboBoxItem).Content.ToString();

                if (str == "一个月")
                {
                    preDate = DateTime.Now.Subtract(new TimeSpan(30, 0, 0, 0, 0));
                }
                else if (str == "两个月")
                {
                    preDate = DateTime.Now.Subtract(new TimeSpan(60, 0, 0, 0, 0));
                }
                else if (str == "半年")
                {
                    preDate = DateTime.Now.Subtract(new TimeSpan(30 * 6, 0, 0, 0, 0));
                }
                else if (str == "全部")
                {
                    preDate = DateTime.MinValue;
                }
                var items = from item in manager.GetIWorkItemsByRequireValues(template)
                            where item.DBTime >= preDate
                            select item;
                if (OnResultArrive != null)
                {
                    OnResultArrive(items);
                }
                this.Close();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
        private void cmbTemplateName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var template = TemplateManager.GetTemplateManager()
                                        .templates[cmbTemplateName.SelectedIndex];

            lstParamters.DataContext = template.mTemplateDetial.mRequireValues.mItem.ToArray();
        }

    }
}
